<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function account_str($str)
{
	//$tmp = $_SERVER['HTTP_COOKIE'];
	//$tmparray = explode(";", $_SERVER['HTTP_COOKIE']);
	//有哪些情况是被动推，不需要加上的
	//1.获得action的值
	$action = 0;
	$tmparray = explode("&", $str);
	for ($i = 0; $i < count($tmparray); $i++)
	{
		if (strpos($tmparray[$i], 'action=') !== false)
		{
			$arr = explode('=', $tmparray[$i]);
			if ($arr[0] == 'action')  //seal_action=...&&action=...
			{
				$action = $arr[1];
				break;
			}
			continue;
		}
	}
	
	//2.根据action的值过滤掉不需加username的情况
	if ($action == 9 || $action == 45 || $action == 54)//心跳包和充值回调的情况还有提问回调
	{
		return $str;
	}
	
	$framework = &get_instance('controller');
	$tmparray = $framework->auth->fields;

	if(11 == $action)
	{
		include  APPPATH . 'languages/zh_CN/common.php';
		$name = $lang['visitor'];
	
		if($name == $tmparray['username'])
		{
			$str .= "&username=admin";
		}
		else
		{
			$str .= "&username=".$tmparray['username'];
		}
	}
	else
	{
		$str .= "&username=".$tmparray['username'];
	}

	return $str;
}

function vpost($url,$timeOut=3,$data=""){ // 模拟提交数据函数

	//
//	$url = account_str($url);

	//print_r($url);
	//exit(0);
//    $url = str_replace("\r\n","<br>", $url);
//    $url = str_replace(" ","%20", $url);
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
//    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
//    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
    //curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
//    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
//    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);///忽略https安全检查
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);///忽略https安全检查
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
	curl_setopt($curl, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain'));// 必须声明请求头
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeOut); // 设置超时限制防止死循环
//    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    $tmpInfo = curl_exec($curl); // 执行操作
	//$err=curl_error($curl);
    if (curl_errno($curl)) {
		#echo 'Errno'.curl_error($curl);//捕抓异常
        curl_close($curl); // 关闭CURL会话
        return false;
    }
    curl_close($curl); // 关闭CURL会话
    return $tmpInfo; // 返回数据
}


//add by sunday 2014/7/1
function myvpost($url,$data="",$timeout = 10)
{
	vpost($url,$timeout,$data);
}

function datapost($url, $data_string) {

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	'Content-Type: application/json; charset=utf-8',
	'Content-Length: ' . strlen($data_string))
	);
	ob_start();
	curl_exec($ch);
	$return_content = ob_get_contents();
	ob_end_clean();

	$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	return array($return_code, $return_content);
}

function iosvpost($url,$timeOut=3,$data=""){ // 模拟提交数据函数

	$url = str_replace("\r\n","<br>", $url);
	$url = str_replace(" ","%20", $url);
	$curl = curl_init(); // 启动一个CURL会话
	curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
	//curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
	curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
	curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
	curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
	curl_setopt($curl, CURLOPT_TIMEOUT, $timeOut); // 设置超时限制防止死循环
	curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
	$tmpInfo = curl_exec($curl); // 执行操作
	if (curl_errno($curl)) {
		//echo 'Errno'.curl_error($curl);//捕抓异常
		curl_close($curl); // 关闭CURL会话
		return FALSE;
	}
	curl_close($curl); // 关闭CURL会话
	return $tmpInfo; // 返回数据
}
?>
